package mainControls;

import smallTools.SimpleTime;
import entities.AcdcProgram;
import entities.Course;
import entities.Section;

public class CoreCourseChecker {
	public CoreCourseChecker() {
	}

	public boolean check(Section theSection, Course theCourse,
			SimpleTime startTimeCandidate) {
		String[] relatedProgramCodes = theCourse.getPrograms();

		for (String programCode : relatedProgramCodes) {
			AcdcProgram program = AcdcProgram.getProgramByCode(programCode);
			String[] coreCourseIds = program.getCourse();

			// is any of the time slots over lap
			if (isAnyOneOverLap(theSection, startTimeCandidate, coreCourseIds) == true)
				return false;
		}
		return true;
	}

	private boolean isAnyOneOverLap(Section theSection,
			SimpleTime startTimeCandidate, String[] coreCourseIds) {

		for (String courseCode : coreCourseIds) {
			Course courseNow = Course.getCourseByCode(courseCode);
			if (conflictWithLecturesOrTutorials(theSection, startTimeCandidate,
					courseNow) == true)
				return true;
		}
		return false;
	}

	private boolean conflictWithLecturesOrTutorials(Section theSection,
			SimpleTime startTimeCandidate, Course courseNow) {
		String[] lectures = courseNow.getAllLecture();
		for (String sectionId : lectures) {
			Section section = Section.getSectionById(sectionId);
			if (section.getSectionStartTime() != null
					&& SimpleTime.areSectionsConflicted(startTimeCandidate,
							theSection.getSectionDuration(),
							section.getSectionStartTime(),
							section.getSectionDuration()))
				return true;
		}

		String[] tutorials = courseNow.getAllTutorial();
		for (String sectionId : tutorials) {
			Section section = Section.getSectionById(sectionId);
			if (section.getSectionStartTime() != null
					&& SimpleTime.areSectionsConflicted(startTimeCandidate,
							theSection.getSectionDuration(),
							section.getSectionStartTime(),
							section.getSectionDuration()))
				return true;
		}
		return false;
	}
}